using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace MyCityCab.Core
{
	/// <summary>
	/// Strongly-typed collection for the Region class.
	/// </summary>
	[Serializable]
	public partial class RegionCollection : ActiveList<Region, RegionCollection> 
	{	   
		public RegionCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Region table.
	/// </summary>
	[Serializable]
	public partial class Region : ActiveRecord<Region>
	{
		#region .ctors and Default Settings
		
		public Region()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Region(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Region(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Region(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Region", TableType.Table, DataService.GetInstance("MyCityCab"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "Id";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = true;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarEnglishName = new TableSchema.TableColumn(schema);
				colvarEnglishName.ColumnName = "EnglishName";
				colvarEnglishName.DataType = DbType.String;
				colvarEnglishName.MaxLength = 50;
				colvarEnglishName.AutoIncrement = false;
				colvarEnglishName.IsNullable = false;
				colvarEnglishName.IsPrimaryKey = false;
				colvarEnglishName.IsForeignKey = false;
				colvarEnglishName.IsReadOnly = false;
				colvarEnglishName.DefaultSetting = @"";
				colvarEnglishName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarEnglishName);
				
				TableSchema.TableColumn colvarLocalName = new TableSchema.TableColumn(schema);
				colvarLocalName.ColumnName = "LocalName";
				colvarLocalName.DataType = DbType.String;
				colvarLocalName.MaxLength = 50;
				colvarLocalName.AutoIncrement = false;
				colvarLocalName.IsNullable = true;
				colvarLocalName.IsPrimaryKey = false;
				colvarLocalName.IsForeignKey = false;
				colvarLocalName.IsReadOnly = false;
				colvarLocalName.DefaultSetting = @"";
				colvarLocalName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLocalName);
				
				TableSchema.TableColumn colvarCountryId = new TableSchema.TableColumn(schema);
				colvarCountryId.ColumnName = "Country_Id";
				colvarCountryId.DataType = DbType.Int32;
				colvarCountryId.MaxLength = 0;
				colvarCountryId.AutoIncrement = false;
				colvarCountryId.IsNullable = false;
				colvarCountryId.IsPrimaryKey = false;
				colvarCountryId.IsForeignKey = true;
				colvarCountryId.IsReadOnly = false;
				colvarCountryId.DefaultSetting = @"";
				
					colvarCountryId.ForeignKeyTableName = "Country";
				schema.Columns.Add(colvarCountryId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MyCityCab"].AddSchema("Region",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("Id")]
		public int Id 
		{
			get { return GetColumnValue<int>("Id"); }

			set { SetColumnValue("Id", value); }

		}

		  
		[XmlAttribute("EnglishName")]
		public string EnglishName 
		{
			get { return GetColumnValue<string>("EnglishName"); }

			set { SetColumnValue("EnglishName", value); }

		}

		  
		[XmlAttribute("LocalName")]
		public string LocalName 
		{
			get { return GetColumnValue<string>("LocalName"); }

			set { SetColumnValue("LocalName", value); }

		}

		  
		[XmlAttribute("CountryId")]
		public int CountryId 
		{
			get { return GetColumnValue<int>("Country_Id"); }

			set { SetColumnValue("Country_Id", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public MyCityCab.Core.LocationCollection LocationRecords()
		{
			return new MyCityCab.Core.LocationCollection().Where(Location.Columns.RegionId, Id).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Country ActiveRecord object related to this Region
		/// 
		/// </summary>
		public MyCityCab.Core.Country Country
		{
			get { return MyCityCab.Core.Country.FetchByID(this.CountryId); }

			set { SetColumnValue("Country_Id", value.Id); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varEnglishName,string varLocalName,int varCountryId)
		{
			Region item = new Region();
			
			item.EnglishName = varEnglishName;
			
			item.LocalName = varLocalName;
			
			item.CountryId = varCountryId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,string varEnglishName,string varLocalName,int varCountryId)
		{
			Region item = new Region();
			
				item.Id = varId;
				
				item.EnglishName = varEnglishName;
				
				item.LocalName = varLocalName;
				
				item.CountryId = varCountryId;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"Id";
			 public static string EnglishName = @"EnglishName";
			 public static string LocalName = @"LocalName";
			 public static string CountryId = @"Country_Id";
						
		}

		#endregion
	}

}

